package com.heima.behavior.service.imp;

import com.alibaba.fastjson.JSONObject;
import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.behavior.vos.ArticleBehaviorVO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

/**
 * @作者 Azer
 * @创建日期 2021/11/27 18:09
 **/
@Service
@Slf4j
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {

        ArticleBehaviorVO behaviorVO=new ArticleBehaviorVO();


//        1. 判断用户是否登录
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser == null) {
//            2. 未登录 直接将 4种行为 均返回false
            behaviorVO.setIsfollow(false);
            behaviorVO.setIslike(false);
            behaviorVO.setIsunlike(false);
            behaviorVO.setIscollection(false);




            return ResponseResult.okResult(behaviorVO);
        }
//
//        3. 如果登录
//        4. 查询行为实体
        Integer apUserId = apUser.getId();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(apUserId, dto.getEquipmentId());

        Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("entryId").is(apBehaviorEntry.getId()));

//        5. 根据行为实体 文章ID查询 是否点赞
        ApLikesBehavior likeOne = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if(likeOne==null){
            behaviorVO.setIslike(false);
        }else {
            behaviorVO.setIslike(true);
        }
//        6. 根据行为实体 文章ID查询 是否不喜欢
        ApUnlikesBehavior UnlikeOne = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        if(UnlikeOne==null){
            behaviorVO.setIsunlike(false);
        }else {
            behaviorVO.setIsunlike(true);
        }

//        7. 根据行为实体 文章ID查询 是否收藏
        ApCollection CollectionOne = mongoTemplate.findOne(query, ApCollection.class);
        if(CollectionOne==null){
            behaviorVO.setIscollection(false);
        }else {
            behaviorVO.setIscollection(true);
        }
//        8. 更具登录用户id 去redis中查询是否关注该作者
        Integer loginId = apUser.getId();
        Integer authorApUserId = dto.getAuthorApUserId();
        Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + loginId, String.valueOf(authorApUserId));
        if(score!=null){
            log.info("已关注");
            behaviorVO.setIsfollow(true);
        }else {
            log.info("未关注");
            behaviorVO.setIsfollow(false);
        }

//        9. 封装结果 返回




        return ResponseResult.okResult(behaviorVO);
    }
}
